"""回调模块
"""
from uuid import uuid4
import json
from app.api.v1.webhook.operate_config import OPERATE,  skipped_push
from flask import request
from flask_restx import Resource
from app.common.transaction_manager import TransactionContext
from app.api.v1.webhook.webhook_manager import WebHookManager

class WebHook(Resource, WebHookManager):
    
    def __init__(self, api=None, *args, **kwargs):
        super(WebHookManager, self).__init__()
    
    def post(self):
        # 获取参数
        exec_status = request.headers.get('X-Job-Success')
        job_uuid = request.headers.get("X-Job-UUID")
        data = json.loads(request.data)
        print(data)
        print(job_uuid)
        print(exec_status)
        trans_context = TransactionContext()
        if exec_status != "true": 
            error =  data.get("error", {})
            # 失败回滚
            trans_context.rollback(job_uuid, error)
        else:
            inventory = data.get("inventory", {})
            trans_context.response_or_request(job_uuid, inventory)

        return {
            "code": 200,
            "data": "操作成功"
        }    
